package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.views.CustomDialog;
import com.hxkcloud.nurse.views.OnRulerChangeListener;
import com.hxkcloud.nurse.views.RulerView;

public class AddBloodpressureActivity extends BaseActivity implements View.OnClickListener{
    private TextView tv_save;
//    private TextView tv_title;
    private TextView tv_hole;
    private TextView tv_low;
    private ImageView iv_back;
    private RulerView rulerView_high;
    private RulerView rulerView_low;
    private double currLoactionMax = 120;
    private double currLocationMin = 90;
    private String bloodMax;
    private String bloodMin;
    private CustomDialog customDialog;
    private String patientId;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private double axMin ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bloodpressure);
        sp = getSharedPreferences("numbers",MODE_PRIVATE);
        editor = sp.edit();
        if (getIntent().hasExtra("bloodMax")){
            bloodMax = getIntent().getStringExtra("bloodMax");
            currLoactionMax = Double.parseDouble(bloodMax);
//            Log.i("--------------->","currLoactionMax:"+currLoactionMax);
        }
        if (getIntent().hasExtra("bloodMin")){
            bloodMin = getIntent().getStringExtra("bloodMin");
            currLocationMin = Double.parseDouble(bloodMin);
        }
        if (getIntent().hasExtra("axMin")){
            axMin =Double.valueOf(getIntent().getStringExtra("axMin"));
//            Log.i("--------------->","axMin:"+axMin);
        }
        if (getIntent().hasExtra("patientId")) {
            patientId = getIntent().getStringExtra("patientId");
        }
        if (sp.getString("pad","")==patientId) {
            if (sp.getInt("high", 0) != -1 || sp.getInt("low", 0) != -1) {
                currLoactionMax = (double)sp.getInt("high", 0);
                currLocationMin = (double)sp.getInt("low", 0);
            }
        }
        initView();
        initData();
        initListener();

    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.ADD_BLOOD_NURSE_SUCCESS:
                dismisssLoading();
                Intent intent = getIntent();
                intent.putExtra("highblood",tv_hole.getText().toString());
                intent.putExtra("lowblood",tv_low.getText().toString());
                editor.putInt("high",Integer.valueOf(tv_hole.getText().toString()));
                editor.putInt("low",Integer.valueOf(tv_low.getText().toString()));
                editor.putString("pad",patientId);
                editor.commit();
                setResult(-1,intent);
                finish();
//                dismisssLoading();
                break;
            case MessageCode.ADD_BLOOD_NURSE_FAILURE:
                dismisssLoading();
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }
                break;
        }

    }

    @Override
    protected void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_title_blood_back);

        tv_save = (TextView) findViewById(R.id.tv_title_blood_func);
        tv_hole = (TextView) findViewById(R.id.bloodpressure_tv_high_num);
        tv_low = (TextView) findViewById(R.id.bloodpressure_tv_low_num);
        rulerView_high = (RulerView) findViewById(R.id.rulerview_high);
        rulerView_low = (RulerView) findViewById(R.id.rulerview_low);
    }

    @Override
    protected void initListener() {
        tv_save.setOnClickListener(this);
        iv_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {
//        if (getIntent().hasExtra("patientId")) {
//            patientId = getIntent().getStringExtra("patientId");
//        }
        tv_save.setText(R.string.xueya_entrings_save);
        rulerView_high.setCurrLocation((int)currLoactionMax);
        rulerView_low.setCurrLocation((int)currLocationMin);
        tv_hole.setText((int)currLoactionMax+"");
        tv_low.setText((int)currLocationMin+"");
        rulerView_high.setOnRulerChangeListener(new OnRulerChangeListener() {
            @Override
            public void onChanged(int newValue) {
                tv_hole.setText(newValue+"");

            }
        });

        rulerView_low.setOnRulerChangeListener(new OnRulerChangeListener() {
            @Override
            public void onChanged(int newValue) {
                tv_low.setText(newValue+"");
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_title_blood_back:
               // if (Integer.valueOf(tv_hole.getText().toString())!=currLoactionMax||Integer.valueOf(tv_low.getText().toString())!=currLocationMin){
                    showDialog();
//                }else {
//                    finish();
//                }
               // finish();
                break;
            case R.id.tv_title_blood_func:
                //保存高低压数值
               // showDialog();
//                if ()
                double holbloodss  = Double.valueOf(tv_hole.getText().toString());
//                double axMinss = Double.valueOf(axMin);
                if (holbloodss>axMin){
//                    Log.i("------>","进入判断");
                    String holblood = tv_hole.getText().toString();
                    String lowblood = tv_low.getText().toString();
                    YiJiaHuNetWorkUtils.AddBlood(patientId,lowblood,holblood,handler);
                }else {
                    toastMsg("收缩压低于或等于最低值"+axMin+",请重新输入");
                }
                break;
        }
    }

    private void showDialog() {
        customDialog = new CustomDialog(mContext);
        customDialog.getWindow().setBackgroundDrawableResource(R.color.alpha);
        customDialog.setOnPositiveListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //传递数据
                double holbloods  = Double.valueOf(tv_hole.getText().toString());
                if (holbloods>axMin) {
                    String holblood = tv_hole.getText().toString();
                    String lowblood = tv_low.getText().toString();
                    YiJiaHuNetWorkUtils.AddBlood(patientId, lowblood, holblood, handler);
                }else {
                    toastMsg("收缩压低于或等于最低值"+axMin+",请重新输入");
                }
                customDialog.dismiss();
            }
        });
        customDialog.setOnNegativeListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                rulerView_high.setCurrLocation(currLoactionMax);
//                rulerView_low.setCurrLocation(currLocationMin);
                customDialog.dismiss();
                finish();
            }
        });
        customDialog.show();

    }


}
